/* Copyright (c) 2016-2020 Enfry Ltd. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Enfry. 
 * You shall not disclose such Confidential Information
 * and shall use it only in accordance with the terms of the agreements
 * you entered into with Enfry.
 */
package com.enfry.common.util.array;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.enfry.common.util.String.Strings;
import com.enfry.common.util.validate.Validates;

/**
 * 转换器工具类
 * @Title com.enfry.wf.util.ConvertUtils
 * @author Penghu
 * @date 2016年10月12日
 */
public class ConvertUtils {

	/**
	 * 将集合转换成字符串
	 * @param c          集合
	 * @param separator	  分隔符
	 * @return
	 * @return String
	 * @throws 
	 * @author Penghu
	 * @date 2016年10月12日
	 */
	public static String toString(Collection<String> c, String separator){

		if (c == null || c.isEmpty())
			return "";
		Iterator<String> iter = c.iterator();
		if(iter != null){
			StringBuilder strb = new StringBuilder().append(iter.next());
			while(iter.hasNext())
				strb.append(separator).append(iter.next());

			return strb.toString();
		}
		return "";
	}

	/**
	 * 把字符串按split分隔转换成字符串数组,去除前后空格
	 * @param strs
	 * @param separator
	 * @return
	 * @return String[]
	 * @throws 
	 * @author Penghu
	 * @date 2016年10月12日
	 */
	public static String[] toStringArray(String strs, String separator){
		if (Validates.isEmpty(strs))
			return new String[0];

		String[] values = strs.split(separator);
		for (int i = 0; i < values.length; i++)
			values[i] = values[i].trim();
		return values;
	}

	/**
	 * 把字符串按split分隔到集合列表,去除前后空格,有序
	 * @param strs
	 * @param separator
	 * @return
	 * @return String[]
	 * @throws 
	 * @author Penghu
	 * @date 2016年10月12日
	 */
	public static Set<String> toStringSet(String strs, String separator){
		Set<String> valueSet = new LinkedHashSet<String>();
		if (Validates.isEmpty(strs))
			return valueSet;
		String[] values = toStringArray(strs, separator);
		for (int i = 0; i < values.length; i++)
			valueSet.add(values[i]);
		return valueSet;
	}

	/**
	 * 把字符串按split分隔到集合列表,去除前后空格
	 * @param strs
	 * @param separator
	 * @return
	 * @return String[]
	 * @throws 
	 * @author Penghu
	 * @date 2016年10月12日
	 */
	public static List<String> toStringList(String strs, String separator){
		List<String> valueList = new ArrayList<String>();
		if (Validates.isEmpty(strs))
			return valueList;
		String[] values = toStringArray(strs, separator);
		for (int i = 0; i < values.length; i++)
			valueList.add(values[i]);
		return valueList;
	}

	/**
	 * 数组转换成分隔符分隔的字符串
	 * 
	 * @param values
	 * @param separator
	 * @return
	 * @return String
	 * @throws 
	 * @author Penghu
	 * @date 2016年11月22日
	 */
	public static String toString(String[] values, String separator)
	{
		if (values == null || values.length == 0)
			return "";

		StringBuilder strb = new StringBuilder(values[0]);
		for (int i=1;i<values.length;i++)
		{
			strb.append(separator).append(values[i]);
		}

		return strb.toString();
	}

	public static List<String> toFieldList(List<Map<String, Object>> list, String field) {
		List<String> idList = new LinkedList<>();
		if(!Validates.isEmpty(list)) {
			for(Map<String, Object> map : list) {
				idList.add(Strings.toString(map.get(field)));
			}
		}
		return idList;
	}

	public static Set<String> toFieldSet(List<Map<String, Object>> list, String field) {
		Set<String> idSet = new HashSet<>();
		if(!Validates.isEmpty(list)) {
			for(Map<String, Object> map : list) {
				idSet.add(Strings.toString(map.get(field)));
			}
		}
		return idSet;
	}

	/**
	 * 将list转为map，支持多个key作为联合主键
	 * @param list
	 * @param keyFieldArr
	 * @return
	 */
	public static Map<String, Map<String, Object>> toCacheMap(List<Map<String, Object>> list, String... keyFieldArr) {
		Map<String, Map<String, Object>> cacheMap = new HashMap<>();
		if(!Validates.isEmpty(list) && !Validates.isEmpty(keyFieldArr)) {
			for(Map<String, Object> map : list) {
				StringBuilder key = new StringBuilder();
				int i=0;
				for(String keyField : keyFieldArr)  {
					if(i > 0) {
						key.append("_");
					}
					i++;
					key.append(Strings.toString(map.get(keyField)));
				}
				cacheMap.put(key.toString(), map);
			}
		}
		return cacheMap;
	}

	/**
	 * 将list分组转成map
	 * @param list
	 * @param key
	 * @return
	 */
	public static Map<String, List<Map<String, Object>>> toCacheList(List<Map<String, Object>> list, String keyField) {
		Map<String, List<Map<String, Object>>> cacheList = new HashMap<>();
		if(!Validates.isEmpty(list) && !Validates.isEmpty(keyField)) {
			for(Map<String, Object> map : list) {
				String key = Strings.toString(map.get(keyField));
				if(cacheList.containsKey(key)) {
					cacheList.get(key).add(map);
				} else {
					List<Map<String, Object>> subList = new LinkedList<>();
					subList.add(map);
					cacheList.put(key, subList);
				}

			}
		}
		return cacheList;
	}
}
